﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html class="x-admin-sm">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta charset="UTF-8">
    <title>菜单权限设置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link href="~/Content/X-admin/css/font.css" rel="stylesheet" />
    <script src="~/Content/X-admin/lib/layui/layui.js"></script>
    <link href="~/Content/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/Content/X-admin/js/jquery.min.js"></script>
    <script src="~/Content/Scripts/System/systemFrame.js"></script>
    <script src="~/Content/Scripts/System/Common.js"></script>
</head>
<body>
    <div style="margin:10px;padding:5px">
        <xblock>
            <button class="layui-btn" onclick="saveMenuPower()"><i class="layui-icon"></i>保存</button>
            <button class="layui-btn" onclick="closeWin()"><i class="layui-icon">&#x1006;</i>取消</button>
        </xblock>     
        <div style="margin-left:3px">
            <table class="layui-hide" id="list" lay-filter="list"></table>

        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    var mCode = "";
    $(function () {
        mCode = getUrlVariable("mCode");
        getMenuCode();
    

        layui.use('form', function () {
            var form = layui.form;
            form.render();
        });
    });
    var powers = "";
    function getMenuCode() {
        $.ajax({
            url: "../System/getMenuByCode",
            data: { menuCode: mCode},
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data.Result) {
                    var d = data.Data[0].operate.split(";");
                    powers = d[0];
                    loadSystemPower();
                } 
            }
        });
    }
 

    /**
      * 系统权限列表
      **/
    function loadSystemPower() {
    
        layui.use('table', function () {
            var table = layui.table;
            table.render({
                elem: '#list'
                , cols: [[ //标题栏
                    { type: 'checkbox' }
                    , { field: 'code', title: '权限编码', width: 120 }
                    , { field: 'name', title: '权限名称', width: 140 }
                    , { field: 'remark', title: '备注说明', width: 150 }
                ]], done: function (res, curr, count) {
                    if (powers == "") {
                        return;
                    }
                    var arr = powers.split(';')[0].split(',');
                    $.each(res['data'], function (i, j) {
                        for (var i = 0; i < arr.length; i++) {
                            if (arr[i] == j["code"]) {
                            j["LAY_CHECKED"] = 'true';
                            var index = j['LAY_TABLE_INDEX'];
                            $('tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
                                $('tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
                                break;
                            }
                        }
                    });
                }
                , height: $(document).height() - $('#list').offset().top - 40
                , url: "../System/getTableList?dataType=sysPower&condition="
                , skin: 'row' //表格风格
                , even: true
                , page: false //是否显示分页
                , limits: [20, 35, 50]
                , limit: 20 //每页默认显示的数量
            });
        });

    }


    /** 
     *  保存权限信息 
     **/
    function saveMenuPower() {
        try {
            var checkStatus = layui.table.checkStatus('list').data;
            var gxStr = [];
            for (var i = 0; i < checkStatus.length; i++) {
                gxStr.push(checkStatus[i].code);
            }
            gxStr = gxStr.join(',');//必须要写，不然后台获取不到数据
            $.ajax({
                url: "../System/saveMenuPower",
                data: { menuCode: mCode, powerValue: gxStr},
                dataType: "json",
                type: "POST",
                success: function (data) {
                    if (data.Result) {
                        layer.alert("保存成功", { icon: 6 }, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(data.Message);
                    }
                }
            });
        } catch (e) {
            alert(e.message);
            window.location = "/Error/Error";
        }
    }
</script>
